package com.ruoyi.system.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.ArticleManagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AdvertisingManagementMapper;
import com.ruoyi.system.domain.AdvertisingManagement;
import com.ruoyi.system.service.IAdvertisingManagementService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 广告管理信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-12
 */
@Service
public class AdvertisingManagementServiceImpl  extends ServiceImpl<AdvertisingManagementMapper,AdvertisingManagement> implements IAdvertisingManagementService
{



    //图片上传路径
    @Value(value = "${path.int_img}")
    private String INT_IMG_PATH;
    //上传图片的访问路径
    @Value(value = "${path.out_img}")
    private String OUT_IMG_URL;

    @Value(value = "${path.http_url}")
    private String OUT_HTTP_URL;


    @Autowired
    private AdvertisingManagementMapper advertisingManagementMapper;

    /**
     * 查询广告管理信息
     *
     * @param id 广告管理信息主键
     * @return 广告管理信息
     */
    @Override
    public AdvertisingManagement selectAdvertisingManagementById(Long id)
    {
        return advertisingManagementMapper.selectAdvertisingManagementById(id);
    }

    @Override
    public int clicksByOne(Integer id) {
        return advertisingManagementMapper.clicksByOne(id);
    }

    /**
     * 查询广告管理信息列表
     *
     * @param advertisingManagement 广告管理信息
     * @return 广告管理信息
     */
    @Override
    public List<AdvertisingManagement> selectAdvertisingManagementList(AdvertisingManagement advertisingManagement)
    {
        return advertisingManagementMapper.selectAdvertisingManagementList(advertisingManagement);
    }

    /**
     * 新增广告管理信息
     *
     * @param advertisingManagement 广告管理信息
     * @return 结果
     */
    @Override
    public Object insertAdvertisingManagement(AdvertisingManagement advertisingManagement) throws IOException {
        //附件信息不为空  图片信息写入指定文件夹  1
        String imgUrl = INT_IMG_PATH;
        String imgPath = OUT_IMG_URL;
        //将封面图片转为Base64
        if (null != advertisingManagement.getFile()) {
            FileUtil(advertisingManagement, imgPath, imgUrl);
//            MultipartFile file = articleManagement.getFile();
//            String imgBase = EncodeFileToBase64(file);
//            articleManagement.setImgBase(imgBase);
        }
        int falg = advertisingManagementMapper.insert(advertisingManagement);
        if (falg >0){
            return  new BaseController().success("添加成功");
        }
        return  new BaseController().error("添加失败");
//        return advertisingManagementMapper.insertAdvertisingManagement(advertisingManagement);
    }


    public void FileUtil(AdvertisingManagement orderFoodDetail, String imgPath, String imgUrl) throws IOException {
        if (null != orderFoodDetail.getFile()) {
            // 获取登录信息
//            Long userId = SecurityUtils.getUserId();
//            // 图片存储路径
//            imgPath += userId + "/";
//            imgUrl += userId + "/";
            // 上传文件名称
//            orderFoodDetail.setImgName(orderFoodDetail.getFile().getOriginalFilename());
            //生成图片名称UUID
            String imgName = UUID.fastUUID().toString();
            String  nameStr = imgName;
            imgPath += imgName;
            imgUrl += imgName;
            //图片写入文件夹

            MultipartFile file = orderFoodDetail.getFile();
            if (file.getContentType().contains("image/png") || file.getContentType().contains("png") || file.getContentType().contains("PNG")) {
                imgPath += ".png";
                imgUrl += ".png";
                nameStr += ".png";
            } else if (file.getContentType().contains("jpg") ||file.getContentType().contains("JPG") || file.getContentType().contains("image/jpeg")) {
                imgPath += ".jpg";
                imgUrl += ".jpg";
                nameStr += ".jpg";
            }else {
                imgPath += ".png";
                imgUrl += ".png";
                nameStr += ".png";
            }

            if (null != imgPath && !imgPath.isEmpty()) {
                File files = new File(imgPath);
                files.mkdirs();
                file.transferTo(files);
                 orderFoodDetail.setImgPath(OUT_HTTP_URL+nameStr);
            }
        }
    }

    /**
     * 修改广告管理信息
     *
     * @param advertisingManagement 广告管理信息
     * @return 结果
     */
    @Override
    public int updateAdvertisingManagement(AdvertisingManagement advertisingManagement)
    {
        return advertisingManagementMapper.updateAdvertisingManagement(advertisingManagement);
    }

    /**
     * 批量删除广告管理信息
     *
     * @param ids 需要删除的广告管理信息主键
     * @return 结果
     */
    @Override
    public int deleteAdvertisingManagementByIds(Long[] ids)
    {
        return advertisingManagementMapper.deleteAdvertisingManagementByIds(ids);
    }

    /**
     * 删除广告管理信息信息
     *
     * @param id 广告管理信息主键
     * @return 结果
     */
    @Override
    public int deleteAdvertisingManagementById(Long id)
    {
        return advertisingManagementMapper.deleteAdvertisingManagementById(id);
    }
}
